package com.fastloan.mgt.model.service.arithmetic.selfsupport;

import com.alibaba.fastjson.JSON;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.model.ApplicationIPC;
import com.fastloan.mgt.application.contract.model.ApplicationContract;
import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.model.annotation.ModelClass;
import com.fastloan.mgt.model.model.ApplicationModel;
import com.fastloan.mgt.model.service.GeneratorModelStrategy;
import com.fastloan.mgt.model.service.arithmetic.selfsupport.weight.SelfSupportCreditModelWeightImpl;
import com.fastloan.mgt.model.uitl.ModelUtil;
import com.kawins.util.IDCardUtil;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Consumer;

import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.NO_PASS;
import static com.fastloan.mgt.model.service.impl.ModelCalculateServiceImpl.PASS;

/**
 * 自营模型
 *
 * @author yanwe
 *         createTime 2017-03-2017/3/9 16:25
 */
@Component
@ModelClass(modelName = "自营模型",modelId = 1)
public class SelfSupportCreditModelCalculate extends GeneratorModelStrategy {

    private static final SelfSupportCreditModelWeightImpl SELF_SUPPORT_CREDIT_MODEL_WEIGHT =
            new SelfSupportCreditModelWeightImpl();

    public SelfSupportCreditModelCalculate() {
        codeFieldMap.put(7,"maritalStatus");
        codeFieldMap.put(20,"economicDependencyNum");
        codeFieldMap.put(9,"local");
        codeFieldMap.put(43,"liveTime");
        codeFieldMap.put(28,"education");
        codeFieldMap.put(75,"manageTime");
        codeFieldMap.put(128,"loanOverdueMonthCount");
        codeFieldMap.put(124,"creditCardMonthCount");
        codeFieldMap.put(146,"taxStatus");
        codeFieldMap.put(41,"bankMoney");
        codeFieldMap.put(150,"stockMoney");
        codeFieldMap.put(144,"manageProductMoney");
        codeFieldMap.put(151,"securityMoney");
        codeFieldMap.put(152,"fundMoney");
        codeFieldMap.put(13,"houseMoney");
        codeFieldMap.put(12,"carMoney");
        codeFieldMap.put(148,"houseMortgageBalance");
        codeFieldMap.put(149,"carMortgageBalance");
        codeFieldMap.put(131,"loanBalance");
        codeFieldMap.put(21,"monthControlMoney");
    }

    @Override
    public void calculate(String applicationId, Consumer<ApplicationModel> applicationModelConsumer) {
        Application application = applicationService.selectByPrimaryKey(applicationId);
        ZyCustomer customer = customerService.selectByPrimaryKey(application.getCustomerId());
        int age = IDCardUtil.getAgeByIDcard(customer.getCardNum());
        SelfSupportCreditModel selfSupportCreditModel = new SelfSupportCreditModel(SELF_SUPPORT_CREDIT_MODEL_WEIGHT);
        selfSupportCreditModel.setAge(age);
        selfSupportCreditModel.setLoanMonth(application.getApplyPeriod());
        List<ApplicationIPC> applicationIPCS = applicationIPCService.selectApplicationIPCNoTree(applicationId);
        convertIPC2Model(selfSupportCreditModel,applicationIPCS);
        BigDecimal creditLine = application.getApplyAmount().min(selfSupportCreditModel.getCreditLine());
        BigDecimal modelResult = ModelUtil.disposeResult(creditLine, 2, BigDecimal.ROUND_UP);
        ApplicationModel applicationModel = createApplicationModel(applicationId);
        applicationModel.setModelResult(modelResult);
        applicationModel.setPass(BigDecimal.ZERO.compareTo(modelResult) == 0 ? NO_PASS : PASS);
        applicationModel.setIpcInfo(JSON.toJSONString(applicationIPCS));
        applicationModelConsumer.accept(applicationModel);
    }

    @Override
    public void sendBack(ApplicationContract applicationContract, boolean isPass) {

    }

}
